package com.hisense.hiioc.hismartplate.service;

import com.alibaba.fastjson.JSONObject;
import com.hisense.hiioc.hismartplate.base.BaseService;
import com.hisense.hiioc.hismartplate.exception.HttpNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class WeatherService {


    //心知天气
    private static final String SeniverseWeatherApi = "https://api.seniverse.com/v3/weather/now.json?key=cd950pcxwxcswdcj&location=guiyang";
    //和风天气
    private static final String HeWeatherApi = "https://free-api.heweather.net/s6/weather?location=贵阳&key=f617da8b305047439627212747dfe23a";

    private static final String HeAirApi = "https://free-api.heweather.net/s6/air/now?location=贵阳&key=f617da8b305047439627212747dfe23a";
    public static final Logger LOGGER = LoggerFactory.getLogger(WeatherService.class);
    @Autowired
    RestTemplate restTemplate;
//    RestTemplate restTemplate = new RestTemplate();

    public JSONObject getWeatherByProxy(){
        JSONObject weatherData = new JSONObject();
        try {
            ResponseEntity<JSONObject> res = restTemplate.getForEntity(HeWeatherApi, JSONObject.class);
            if(200!=res.getStatusCodeValue())
                throw new HttpNotFoundException();
            JSONObject plainWeatherData = res.getBody().getJSONArray("HeWeather6").getJSONObject(0).getJSONObject("now");
            weatherData.put("txt",plainWeatherData.get("cond_txt"));
            weatherData.put("tmp",plainWeatherData.get("tmp"));
            weatherData.put("fromApi","HeWeatherByProxy");
        }catch (Exception e){
            try{
                e.printStackTrace();
                ResponseEntity<JSONObject> res = restTemplate.getForEntity(SeniverseWeatherApi, JSONObject.class);
                if(200!=res.getStatusCodeValue())
                    throw new HttpNotFoundException();
                JSONObject plainWeatherData = res.getBody().getJSONArray("results").getJSONObject(0).getJSONObject("now");
                weatherData.put("txt",plainWeatherData.get("text"));
                weatherData.put("tmp",plainWeatherData.get("temperature"));
                weatherData.put("fromApi","SeniverseWeatherByProxy");
            }catch (Exception ex){
                ex.printStackTrace();
                throw new HttpNotFoundException();
            }

        }
        return weatherData;
    }
    public JSONObject getWeatherWithoutProxy(){
        JSONObject weatherData = new JSONObject();
        RestTemplate resT = new RestTemplate();
        try {
            ResponseEntity<JSONObject> res = resT.getForEntity(HeWeatherApi, JSONObject.class);
            if(200!=res.getStatusCodeValue())
                throw new HttpNotFoundException();
            JSONObject plainWeatherData = res.getBody().getJSONArray("HeWeather6").getJSONObject(0).getJSONObject("now");
            weatherData.put("txt",plainWeatherData.get("cond_txt"));
            weatherData.put("tmp",plainWeatherData.get("tmp"));
            weatherData.put("fromApi","HeWeatherWithoutProxy");
        }catch (Exception e){
            try{
                e.printStackTrace();
                ResponseEntity<JSONObject> res = resT.getForEntity(SeniverseWeatherApi, JSONObject.class);
                if(200!=res.getStatusCodeValue())
                    throw new HttpNotFoundException();
                JSONObject plainWeatherData = res.getBody().getJSONArray("results").getJSONObject(0).getJSONObject("now");
                weatherData.put("txt",plainWeatherData.get("text"));
                weatherData.put("tmp",plainWeatherData.get("temperature"));
                weatherData.put("fromApi","SeniverseWeatherWithoutProxy");
            }catch (Exception ex){
                ex.printStackTrace();
                throw new HttpNotFoundException();
            }

        }
        return weatherData;
    }
    public JSONObject getAirWithoutProxy(){
        JSONObject weatherData = new JSONObject();
        RestTemplate resT = new RestTemplate();
        try {
            ResponseEntity<JSONObject> res = resT.getForEntity(HeAirApi, JSONObject.class);
            if(200!=res.getStatusCodeValue())
                throw new HttpNotFoundException();
            JSONObject plainWeatherData = res.getBody().getJSONArray("HeWeather6").getJSONObject(0).getJSONObject("air_now_city");
            weatherData.put("aqi",plainWeatherData.get("aqi"));
            weatherData.put("quality",plainWeatherData.get("qlty"));
            weatherData.put("pm25",plainWeatherData.get("pm25"));
        }catch (Exception e){
            //心知天气暂无空气API
            /*try{
                e.printStackTrace();
                ResponseEntity<JSONObject> res = resT.getForEntity(SeniverseWeatherApi, JSONObject.class);
                if(200!=res.getStatusCodeValue())
                    throw new HttpNotFoundException();
                JSONObject plainWeatherData = res.getBody().getJSONArray("results").getJSONObject(0).getJSONObject("now");
                weatherData.put("txt",plainWeatherData.get("text"));
                weatherData.put("tmp",plainWeatherData.get("temperature"));
                weatherData.put("fromApi","SeniverseWeatherWithoutProxy");
            }catch (Exception ex){
                ex.printStackTrace();
                throw new HttpNotFoundException();
            }*/

        }
        return weatherData;
    }
    public JSONObject getWeather(){
        JSONObject weatherData = new JSONObject();
        try{
            weatherData = getWeatherWithoutProxy();
        }catch (Exception e ){
            /*try {
                weatherData = getWeatherByProxy();
            }catch (Exception ex){
                ex.printStackTrace();
            }*/
        }
        return weatherData;
    }
    public JSONObject getAir(){
        JSONObject weatherData = new JSONObject();
        try{
            weatherData = getAirWithoutProxy();
        }catch (Exception e){
            e.printStackTrace();
        }
        return weatherData;
    }


}
